package com.xxg.renrensearch.controller;

import com.xxg.renrensearch.dao.Product;
import com.xxg.renrensearch.domain.query.*;
import com.xxg.renrensearch.domain.result.*;
import com.xxg.renrensearch.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

/**
 * 商品搜索接口
 *
 * @author xuxiaogang
 * @date 2021-11-24
 */
@Api(tags = "商品搜索接口")
@RestController
@RequestMapping("/productSearch")
public class ProductSearchController {
    private final ProductService productService;

    public ProductSearchController(ProductService productService) {
        this.productService = productService;
    }

    @ApiOperation("根据spuId查询商品信息")
    @RequestMapping(value = "findProductBySpuId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "getProductQuery", value = "查询条件", required = true, dataType = "GetProductQuery")})
    public BaseResult<List<Product>> findProductBySpuId(@RequestBody @Validated GetProductQuery getProductQuery) {
        return productService.findProductBySpuId(getProductQuery.getSpuId());
    }

    @ApiOperation("根据店铺ID[商品分类，带货权限，带货状态]排序字段分页查询商品信息")
    @RequestMapping(value = "findOrderedProductByShopId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "getProductByOrderTypeQuery", value = "查询条件", required = true, dataType = "GetProductByOrderTypeQuery")})
    public BaseResult<List<Product>> findOrderedProductByShopId(@RequestBody @Validated GetProductByOrderTypeQuery getProductByOrderTypeQuery, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        getProductByOrderTypeQuery.setMyShopId(Integer.parseInt(shopId));
        return productService.findOrderedProductByShopId(getProductByOrderTypeQuery);
    }

    @ApiOperation("根据[商品分类，商品是否采集，带货权限]排序字段分页查询我的商品信息")
    @RequestMapping(value = "findMyOrderedProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "getMyProductQuery", value = "查询条件", required = true, dataType = "GetMyProductQuery")})
    public BaseResult<List<Product>> findMyOrderedProduct(@RequestBody @Validated GetMyProductQuery getMyProductQuery, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        getMyProductQuery.setShopId(Integer.parseInt(shopId));
        return productService.findMyOrderedProduct(getMyProductQuery);
    }

    @ApiOperation("根据关键词[店铺ID,商品分类]分页查询商品信息")
    @RequestMapping(value = "findProductByKeyword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetProductByKeyWordQuery")})
    public BaseResult<List<Product>> findProductByKeyword(@RequestBody @Validated GetProductByKeyWordQuery query) {
        return productService.findProductByKeyword(query);
    }

    @ApiOperation("根据关键词[商品分类]分页查询我的商品信息")
    @RequestMapping(value = "findMyProductByKeyword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetProductByKeyWordQuery")})
    public BaseResult<List<Product>> findMyProductByKeyword(@RequestBody @Validated GetProductByKeyWordQuery query, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        query.setShopId(Integer.parseInt(shopId));
        return productService.findProductByKeyword(query);
    }

    @ApiOperation("获取我的商品、已带货、被带货商品数量")
    @RequestMapping(value = "findMyProductCounts", method = RequestMethod.GET)
    public BaseResult<ProductCountsResult> findMyProductCounts(HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        return productService.findProductCountsByShopId(Integer.parseInt(shopId));
    }

    @ApiOperation("获取其他店铺的商品、已带货、被带货商品数量")
    @RequestMapping(value = "findOtherProductCounts", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId", value = "店铺id", required = true, dataType = "Integer", paramType = "query")})
    public BaseResult<ProductCountsResult> findOtherProductCounts(@RequestParam("shopId") Integer shopId) {
        if (shopId == null || shopId <= 0) {
            return new BaseResult<>(null);
        }
        return productService.findProductCountsByShopId(shopId);
    }

    @ApiOperation("获取spu的被带货，被采集数量")
    @RequestMapping(value = "findProductCountsBySpu", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "spuId", value = "spuId", required = true, dataType = "Integer", paramType = "query")})
    public BaseResult<ProductCountsBySpuResult> findProductCountsBySpu(@RequestParam("spuId") Integer spuId) {
        if (spuId == null || spuId <= 0) {
            return new BaseResult<>(null);
        }
        return productService.findProductCountsBySpu(spuId);
    }

    @ApiOperation("以我的角度分页查询我已带货商品信息，按照店铺进行分组")
    @RequestMapping(value = "findCarriedProductGroupByShop", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetCarriedProductQuery")})
    public BaseResult<List<ShopWithProducts>> findCarriedProductGroupByShop(@RequestBody @Validated GetCarriedProductQuery query, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        query.setShopId(Integer.parseInt(shopId));
        return productService.findCarriedProductGroupByShop(query);
    }

    @ApiOperation("以我的角度分页查询我被带货商品信息，按照店铺进行分组")
    @RequestMapping(value = "findBeCarriedProductGroupByShop", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetCarriedProductQuery")})
    public BaseResult<List<ShopWithProducts>> findBeCarriedProductGroupByShop(@RequestBody @Validated GetCarriedProductQuery query, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        query.setShopId(Integer.parseInt(shopId));
        return productService.findBeCarriedProductGroupByShop(query);
    }

    @ApiOperation("根据关键词分页查询我已带货商品")
    @RequestMapping(value = "findMyCarriedProductByKeyword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetProductByKeyWordQuery2")})
    public BaseResult<List<Product>> findMyCarriedProductByKeyword(@RequestBody @Validated GetProductByKeyWordQuery2 query, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        query.setShopId(Integer.parseInt(shopId));
        return productService.findMyCarriedProductByKeyword(query);
    }

    @ApiOperation("根据关键词分页查询我被带货商品")
    @RequestMapping(value = "findMyBeCarriedProductByKeyword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetProductByKeyWordQuery2")})
    public BaseResult<List<Product>> findMyBeCarriedProductByKeyword(@RequestBody @Validated GetProductByKeyWordQuery2 query, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        query.setShopId(Integer.parseInt(shopId));
        return productService.findMyBeCarriedProductByKeyword(query);
    }

    @ApiOperation("根据店铺分页查看已带货商品列表")
    @RequestMapping(value = "findMyCarriedProductByShop", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetCarriedProductQuery2")})
    public BaseResult<List<Product>> findMyCarriedProductByShop(@RequestBody @Validated GetCarriedProductQuery2 query, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        query.setShopId(Integer.parseInt(shopId));
        return productService.findMyCarriedProductByShop(query);
    }

    @ApiOperation("根据店铺分页查看被带货商品列表")
    @RequestMapping(value = "findBeMyCarriedProductByShop", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询条件", required = true, dataType = "GetCarriedProductQuery2")})
    public BaseResult<List<Product>> findBeMyCarriedProductByShop(@RequestBody @Validated GetCarriedProductQuery2 query, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        query.setShopId(Integer.parseInt(shopId));
        return productService.findBeMyCarriedProductByShop(query);
    }

    @ApiOperation("以图搜图")
    @RequestMapping(value = "searchByPic", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "file", paramType = "form", value = "图片文件", required = true, dataType = "file")})
    public BaseResult<List<Product>> imageSearch(@RequestPart MultipartFile file, HttpServletRequest request) {
        long size = 4194304;
        if (file.getSize() > size) {
            return new BaseResult<>(ReturnCodeEnum.INVALID_PARAMETER, "图片大小不能超过4MB", null);
        }
        // 检查是否时图片
        BufferedImage bi;
        byte[] image;
        try {
            bi = ImageIO.read(file.getInputStream());
            image = file.getBytes();
        } catch (IOException e) {
            return new BaseResult<>(ReturnCodeEnum.INVALID_PARAMETER, e.getMessage(), null);
        }
        if (bi == null) {
            return new BaseResult<>(ReturnCodeEnum.INVALID_PARAMETER, "上传的文件不是图片", null);
        }
        return productService.searchByPic(image);
    }
}
